package common

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	mpPb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	commonService "git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type MpController struct {
	controller.BaseController
	miniService commonService.MiniProgramServiceIface
}

// NewMpController 开放接口
func NewMpController(miniService commonService.MiniProgramServiceIface) *MpController {
	return &MpController{
		miniService: miniService,
	}
}

func (c *MpController) RegisterRouter(g *gin.RouterGroup) {
	// 省市雷达商机类型筛选项接口
	g.POST("get-qrcode", c.getQrCode)
}

type getQrCodeRequest struct {
	Scene      string `json:"scene" binding:"required" err:"scene参数不能为空"`
	Page       string `json:"page"`
	CheckPath  bool   `json:"check_path"`
	EnvVersion string `json:"env_version"`
	Width      int32  `json:"width"`
	AutoColor  bool   `json:"auto_color"`
	IsHyaline  bool   `json:"is_hyaline"`
}

func (c *MpController) getQrCode(ctx *gin.Context) {
	var request getQrCodeRequest
	if err := ctx.ShouldBind(&request); err != nil {
		stark.Logger.Errorf(ctx, "http getQrCode 参数错误：%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.miniService.GetQrCode(ctx, &mpPb.GetQRCodeRequest{
		Scene:      request.Scene,
		Page:       request.Page,
		CheckPath:  request.CheckPath,
		EnvVersion: request.EnvVersion,
		Width:      request.Width,
		AutoColor:  request.AutoColor,
		IsHyaline:  request.IsHyaline,
	})
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, map[string]string{"qr_code_url": resp.QrCodeUrl})
}
